package tianrun.ziguan.api.calculate.config.utils;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import tianrun.ziguan.api.calculate.config.config.exception.ErrorCode;
import tianrun.ziguan.api.common.core.base.ReturnMessage;

public class HttpResponseTools {
    private static final String ERROR_RESULT = "ERROR";

    private HttpResponseTools() {

    }

    public static ResponseEntity<ReturnMessage> good() {
        return new ResponseEntity<>(new ReturnMessage(), HttpStatus.OK);
    }

    public static ResponseEntity<ReturnMessage> good(String msg) {
        return new ResponseEntity<>(ReturnMessageUtil.success(msg), HttpStatus.OK);
    }

    public static <T> ResponseEntity<T> good(T t) {
        return new ResponseEntity<>(t, HttpStatus.OK);
    }

    public static ResponseEntity<ReturnMessage> bad(Exception e, ErrorCode c) {
        return new ResponseEntity<>(ReturnMessageUtil.error(e, c), HttpStatus.OK);
    }

    public static ResponseEntity<ReturnMessage> bad(ErrorCode c) {
        return new ResponseEntity<>(ReturnMessageUtil.error(c), HttpStatus.OK);
    }

    public static class ReturnMessageUtil {
        private static final String ERROR_RESULT = "ERROR";

        private ReturnMessageUtil() {
        }

        public static ReturnMessage success(String m) {
            ReturnMessage returnMessage = new ReturnMessage();
            returnMessage.setMessage(m);
            return returnMessage;
        }

        public static ReturnMessage error(Exception e, ErrorCode c) {
            ReturnMessage returnMessage = error(c);
            returnMessage.setMessage(e.getMessage());
            return returnMessage;
        }

        public static ReturnMessage error(ErrorCode errorCode) {
            ReturnMessage returnMessage = new ReturnMessage();
            returnMessage.setMessage(errorCode.getDescription());
            returnMessage.setStatusCode(errorCode.getCode());
            returnMessage.setResult(ERROR_RESULT);
            return returnMessage;
        }

        public static <T extends ReturnMessage> T error(Class<T> clazz, ErrorCode errorCode) {
            ReturnMessage returnMessage = new ReturnMessage();
            returnMessage.setMessage(errorCode.getDescription());
            returnMessage.setStatusCode(errorCode.getCode());
            returnMessage.setResult(ERROR_RESULT);
            return JSONObject.parseObject(JSONObject.toJSONString(returnMessage), clazz);
        }

        public static <T extends ReturnMessage> T error(TypeReference<T> type, Exception e, ErrorCode c) {
            ReturnMessage returnMessage = new ReturnMessage();
            returnMessage.setMessage(e.getMessage());
            returnMessage.setStatusCode(c.getCode());
            returnMessage.setResult(ERROR_RESULT);
            return JSONObject.parseObject(JSONObject.toJSONString(returnMessage), type);
        }

        public static <T extends ReturnMessage> T error(TypeReference<T> type, ErrorCode errorCode) {
            ReturnMessage returnMessage = new ReturnMessage();
            returnMessage.setMessage(errorCode.getDescription());
            returnMessage.setStatusCode(errorCode.getCode());
            returnMessage.setResult(ERROR_RESULT);
            return JSONObject.parseObject(JSONObject.toJSONString(returnMessage), type);
        }
    }
}

